package com.yy.launchercaijing

import android.content.pm.ActivityInfo
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.Collections.emptyList

class AppsActivity : AppCompatActivity() {
    private lateinit var recyclerView: RecyclerView
    private lateinit var appAdapter: AppAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_apps)
        enableFullScreen()

        findViewById<ImageView>(R.id.imgv_back).setOnClickListener {
            onBackPressed()
        }
         recyclerView = findViewById(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)




        // 设置列数，根据屏幕方向动态设置
        val spanCount = if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            6 // 横屏显示 6 列
        } else {
            4 // 竖屏显示 4 列
        }

        recyclerView.layoutManager = GridLayoutManager(this, spanCount)
        appAdapter = AppAdapter(emptyList())
        recyclerView.adapter = appAdapter

        // 在子线程中加载应用信息
        loadInstalledAppsAsync()
    }

    override fun onResume() {
        super.onResume()
        enableFullScreen()
    }

    private fun loadInstalledAppsAsync() {
        // 启动一个协程在子线程中加载应用信息
        CoroutineScope(Dispatchers.IO).launch {
            val apps = getInstalledApps()

            // 切换到主线程更新 UI
            withContext(Dispatchers.Main) {
                appAdapter.updateData(apps)
            }
        }
    }

    private fun enableFullScreen() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//            // Android 11 及以上版本
//            window.insetsController?.let { controller ->
//                controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
//                controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
//            }
//        } else {
//            // Android 11 以下版本
//            window.decorView.systemUiVisibility = (
//                    View.SYSTEM_UI_FLAG_FULLSCREEN
//                            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                            or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
//                    )
//        }
    }

    private fun getInstalledApps(): List<AppInfo> {
        val appList = mutableListOf<AppInfo>()
        val packageManager: PackageManager = packageManager
        val installedApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)

        for (app in installedApps) {
            // 过滤掉系统应用
           // if ((app.flags and ApplicationInfo.FLAG_SYSTEM) == 0) {
               if(app.packageName != "com.yy.launchercaijing") {
                   var packageName = app.packageName
                   var lintent = packageManager.getLaunchIntentForPackage(packageName)
                   val appName = packageManager.getApplicationLabel(app).toString()
                   val appIcon = packageManager.getApplicationIcon(app)
                   if(lintent == null) continue
                   appList.add(AppInfo(packageName, lintent!!, appName, appIcon))
               }
           // }
        }
        return appList
    }


}